//
//  JMEnrollmentCell.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/7.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMEnrollmentCell.h"
#import "JMHeader.h"
#import "UIViewExt.h"
#import <UIImageView+WebCache.h>
#import "NSString+date.h"

@interface JMEnrollmentCell (){

    NSMutableArray *dataArray;
}

@property (nonatomic, strong) NSMutableArray *labelArray;
@property (nonatomic, strong) UIImageView *lineImageView;

@property (nonatomic, strong) UIImageView *bottomLineImageView;

@end

@implementation JMEnrollmentCell

- (NSMutableArray *)labelArray
{
    if (!_labelArray) {
        _labelArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _labelArray;
}

- (UIImageView *)bottomLineImageView
{
    if (!_bottomLineImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"projectImage.bundle/line_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        _bottomLineImageView = imageView;
        [self addSubview:_bottomLineImageView];
    }
    return _bottomLineImageView;
}


- (UIImageView *)logoImageView
{
    if (!_logoImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"<#imageName#>"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = UIColorFromRGB(0xc4c4c4);
        _logoImageView = imageView;
        [self addSubview:_logoImageView];
    }
    return _logoImageView;
}

- (UIImageView *)lineImageView
{
    if (!_lineImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"projectImage.bundle/line_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = UIColorFromRGB(0xc4c4c4);
        _lineImageView = imageView;
        [self addSubview:_lineImageView];
    }
    return _lineImageView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.font = [UIFont systemFontOfSize:14.0f];
        _nameLabel = label;
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)infoLabel
{
    if (!_infoLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = UIColorFromRGB(0x494949);
        label.font = [UIFont systemFontOfSize:13.0f];
        _infoLabel = label;
        [self addSubview:_infoLabel];
    }
    return _infoLabel;
}


- (void)layoutSubviews{
    [super layoutSubviews];

    self.logoImageView.frame = CGRectMake(UI_SCREEN_WITH *0.039025,UI_SCREEN_WITH *0.039025, UI_SCREEN_WITH *0.1609375, UI_SCREEN_WITH *0.1609375);
    self.nameLabel.frame =CGRectMake(self.logoImageView.frameRight + UI_SCREEN_WITH *0.015625, self.logoImageView.frameTop, UI_SCREEN_WITH * 0.3025, UI_SCREEN_WITH *0.06718);
    self.lineImageView.frame = CGRectMake(4, UI_SCREEN_WITH *0.234375, UI_SCREEN_WITH- 4, 0.6);
    CGSize size = [self.infoLabel.text sizeWithFont:[UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125] andMaxSize:CGSizeMake(320, UI_SCREEN_WITH *0.21875 - 4)];
    self.infoLabel.frame = CGRectMake(self.lineImageView.frameLeft, self.lineImageView.frameBottom + 4, self.lineImageView.frameWidth,size.height);
    
    float leftPoin = self.nameLabel.frameLeft;

    [self.labelArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for(int i =0 ;i <dataArray.count;i++){
        NSString *title = dataArray[i];
        CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125] andMaxSize:CGSizeMake(320, UI_SCREEN_WITH *0.0625)];
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(leftPoin, self.nameLabel.frameBottom + 10, size.width + 5,UI_SCREEN_WITH *0.046875)];
        la.text = title;
        la.layer.masksToBounds =YES;
        la.layer.cornerRadius = 3.0;
        la.textAlignment =NSTextAlignmentCenter;
        la.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125];
        la.backgroundColor = UIColorFromRGB(0xe9e9e9);
        la.textColor = UIColorFromRGB(0x939393);
        [self addSubview:la];
        leftPoin =la.frameRight + 3;
        [self.labelArray addObject:la];
    }

    
    self.bottomLineImageView.frame = CGRectMake(0, self.frameHeight - 1, UI_SCREEN_WITH, 1);
}


-(void)upCellInfoWith:(JMPolicyMode *)mode{
    
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:mode.logoString]];
    self.nameLabel.text = mode.titleString;
    self.infoLabel.text = mode.policyString;
    NSString *str = [mode.levelString stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    NSString * theString = [filteredArray componentsJoinedByString:@" "];
    
    NSMutableArray * array = (NSMutableArray *)[theString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"[]"]];
    for(int i = 0; i<array.count;i++){
        NSString *subStr =array[i];
        if(subStr.length == 0){
            [array removeObject:subStr];
        }
    }
    dataArray = array;
    [self layoutIfNeeded];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
